package com.jaeger.ninegridimgdemo;

import com.bumptech.glide.Glide;
import com.jaeger.ninegridimageview.NineGridImageView;
import com.jaeger.ninegridimageview.NineGridImageViewAdapter;
import com.jaeger.ninegridimgdemo.entity.Post;

import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.List;

import static com.jaeger.ninegridimageview.NineGridImageView.STYLE_GRID;

/**
 * 列表适配类
 */
public class PostAdapter extends BaseItemProvider {
    private Context mContext;
    private List<Post> mData;
    private int mShowStyle = STYLE_GRID;

    public PostAdapter(Context mContext, List<Post> data, int showStyle) {
        this.mContext = mContext;
        this.mData = data;
        this.mShowStyle = showStyle;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Component itemContainer;
        if (this.mShowStyle == NineGridImageView.STYLE_FILL) {
            itemContainer = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_post_fill_style, componentContainer, false);
        } else {
            itemContainer = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_post_grid_style, componentContainer, false);
        }
        Text title = (Text) itemContainer.findComponentById(ResourceTable.Id_tv_content);
        title.setText(mData.get(position).getContent());
        NineGridImageView<String> mNglContent = (NineGridImageView<String>) itemContainer.findComponentById(ResourceTable.Id_ngl_images);
        NineGridImageViewAdapter nineGridAdapter = createNineGridAdapter();
        mNglContent.setAdapter(nineGridAdapter);
        mNglContent.setImagesData(mData.get(position).getImgUrlList(), mData.get(position).getmSpanType());
        return itemContainer;
    }

    /**
     * 简易实现类
     */
    public static class NineGridAdapter extends NineGridImageViewAdapter<String> {
        @Override
        protected void onDisplayImage(Context context, Image imageView, String url) {
                Glide.with(context).load(url).into(imageView);
        }
    }

    /**
     * 实现一个NineGridImageViewAdapter
     * @return NineGridImageViewAdapter返回
     */
    public NineGridImageViewAdapter createNineGridAdapter() {
        NineGridImageViewAdapter<String> nineGridAdapter = new NineGridImageViewAdapter<String>() {
            @Override
            protected void onDisplayImage(Context context, Image imageView, String url) {
                Glide.with(context)
                        .load(url)
                        .placeholder(ResourceTable.Media_ic_default_image)
                        .into(imageView);
            }

            @Override
            protected Image generateImageView(Context context) {
                return super.generateImageView(context);
            }

            @Override
            protected void onItemImageClick(Context context, Image imageView, int index, List<String> list) {
                new ToastDialog(context).setText("image position is " + index).setDuration(2000).show();
            }

            @Override
            protected boolean onItemImageLongClick(Context context, Image imageView, int index, List<String> list) {
                new ToastDialog(context).setText("image long click position is " + index).setDuration(2000).show();
                return true;
            }
        };
        return nineGridAdapter;
    }

}
